home *** CD-ROM | disk | FTP | other *** search
/ CU Amiga Super CD-ROM 17 / CU Amiga Magazine's Super CD-ROM 17 (1997)(EMAP Images)(GB)[!][issue 1997-12].iso / CUCD / Programming / DiceSource / master / Examples / CC / subs.c < prev   
C/C++ Source or Header  |  1994-02-01  |  3KB  |  207 lines

  1.  
  2. /*
  3.  *  SUBS.C
  4.  *
  5.  *  (c)Copyright 1990, All Rights Reserved
  6.  */
  7.  
  8. #include "defs.h"
  9. #ifdef AMIGA
  10. #include <exec/execbase.h>
  11. #endif
  12.  
  13. Prototype int ExtArgsEnv(short, char ***, char *);
  14. Prototype int ExtArgsFile(short, char ***, char *);
  15. Prototype int ExtArgsBuf(short, char ***, char *, long);
  16.  
  17. Prototype char *skipspace(char *);
  18. Prototype char *skipnspace(char *);
  19. Prototype void CreateObjPath(char *);
  20.  
  21. #ifdef AMIGA
  22. extern struct ExecBase *SysBase;
  23. #endif
  24.  
  25. /*
  26.  *  ExtArgsEnv()    DCCOPTS
  27.  */
  28.  
  29. #ifdef AMIGA
  30.  
  31. int
  32. ExtArgsEnv(short ac, char ***pav, char *envname)
  33. {
  34.     long len = -1;
  35.     char *str;
  36.     int nac = 0;
  37.  
  38. #ifndef LATTICE
  39.     if (SysBase->LibNode.lib_Version < 36) {
  40. #else
  41.     {
  42. #endif
  43.     char buf[64];
  44.  
  45.     sprintf(buf, "ENV:%s", envname);
  46.     return(ExtArgsFile(ac, pav, buf));
  47.     }
  48. #ifndef LATTICE
  49.       else {
  50.     str = malloc(1024);
  51.     len = GetVar(envname, str, 1024, 0);
  52.     if (len > 0)
  53.         str = realloc(str, len + 1);
  54.     else
  55.         free(str);
  56.     return(ExtArgsBuf(ac, pav, str, len));
  57.     }
  58. #endif
  59. }
  60.  
  61. #else
  62.  
  63. int
  64. ExtArgsEnv(short ac, char ***pav, char *envname)
  65. {
  66.     char *str;
  67.     short len;
  68.  
  69.     if (str = getenv(envname)) {
  70.     len = strlen(str);
  71.     str = realloc(str, len + 1);
  72.     return(ExtArgsBuf(ac, pav, str, len));
  73.     }
  74.     return(ac);
  75. }
  76.  
  77. #endif
  78.  
  79. int
  80. ExtArgsFile(short ac, char ***pav, char *file)
  81. {
  82.     int fd;
  83.     long len = -1;
  84.     char *str = NULL;
  85.  
  86.     if ((fd = open(file, O_RDONLY)) >= 0) {
  87.     if ((len = lseek(fd, 0L, 2)) > 0) {
  88.         str = malloc(len + 1);
  89.  
  90.         lseek(fd, 0L, 0);
  91.         read(fd, str, len);
  92.         str[len] = 0;
  93.     }
  94.     close(fd);
  95.     }
  96.     return(ExtArgsBuf(ac, pav, str, len));
  97. }
  98.  
  99. int
  100. ExtArgsBuf(short ac, char ***pav, char *str, long len)
  101. {
  102.     char *ptr;
  103.     int nac = 0;
  104.     char **nav;
  105.  
  106.     if (len < 0)
  107.     return(ac);
  108.  
  109.     /*
  110.      *    parse
  111.      */
  112.  
  113.     ptr = skipspace(str);
  114.     while (*ptr) {
  115.     ++nac;
  116.     ptr = skipnspace(ptr);
  117.     ptr = skipspace(ptr);
  118.     }
  119.     nav = malloc((ac + nac + 1) * sizeof(char *));
  120.     movmem(*pav, nav, ac * sizeof(char *));
  121.     nac = ac;
  122.     ptr = skipspace(str);
  123.     while (*ptr) {
  124.     nav[nac] = ptr;
  125.     ptr = skipnspace(ptr);
  126.     if (*ptr)
  127.         *ptr++ = 0;
  128.     ptr = skipspace(ptr);
  129.     ++nac;
  130.     }
  131.     nav[nac] = NULL;
  132.     ac = nac;
  133.     *pav = nav;
  134.     return(ac);
  135. }
  136.  
  137. char *
  138. skipspace(ptr)
  139. char *ptr;
  140. {
  141.     while (*ptr == '\n' || *ptr == ' ' || *ptr == 9)
  142.     ++ptr;
  143.     return(ptr);
  144. }
  145.  
  146. char *
  147. skipnspace(ptr)
  148. char *ptr;
  149. {
  150.     while (*ptr != '\n' && *ptr != ' ' && *ptr != 9 && *ptr)
  151.     ++ptr;
  152.     return(ptr);
  153. }
  154.  
  155. /*
  156.  *  check for path existance
  157.  */
  158.  
  159. void
  160. CreateObjPath(file)
  161. char *file;
  162. {
  163.     short i;
  164.     short j;
  165. #ifdef AMIGA
  166.     BPTR lock;
  167. #else
  168.     struct stat statBuf;
  169. #endif
  170.     char tmp[128];
  171.  
  172.     for (i = strlen(file); i >= 0 && file[i] != '/' && file[i] != ':'; --i);
  173.  
  174.     if (i <= 0)
  175.     return;
  176.     strncpy(tmp, file, i);
  177.     tmp[i] = 0;
  178.  
  179.     /*
  180.      *    valid directory
  181.      */
  182.  
  183. #ifdef AMIGA
  184.     if (lock = Lock(tmp, SHARED_LOCK)) {
  185.     UnLock(lock);
  186.     return;
  187.     }
  188. #else
  189.     if (stat(tmp, &statBuf) == 0)
  190.     return;
  191. #endif
  192.  
  193.     /*
  194.      *    invalid, attempt to create directory path.
  195.      */
  196.  
  197.     for (j = 0; j <= i; ++j) {
  198.     if (file[j] == '/') {
  199.         strncpy(tmp, file, j);
  200.         tmp[j] = 0;
  201.         if (mkdir(tmp/*, 0666*/) < 0 && errno != EEXIST)
  202.         break;
  203.     }
  204.     }
  205. }
  206.  
  207.